<?php if (!defined("BASEPATH")) exit("No direct script access allowed");

class Bwin extends MY_Controller {

	private $insertedOdds = 0;

	function __construct() {
		parent::__construct();
	}
	
	function index() {
		$this->odds_model->clearOdds(strtolower(get_class()));
		$getHtmlByBookie = $this->html_model->getHtmlByBookie(strtolower(get_class()));
		$getSzotar = current($this->szotar_model->getSzotar(strtolower(get_class())));
		//Common::pr($getSzotar);
		//Common::pr(count($getHtmlByBookie)); exit;
		if(count($getHtmlByBookie)) {
			foreach($getHtmlByBookie as $currhtml) {
				$this->benchmark->mark("#".$currhtml["html_id"]."_start");
				$this->insertedOdds = 0;
				$html = str_get_html($currhtml["html_text"]);
				
				$widget_contents = $html->find("div[class=ui-widget-content-body]"); 

				$insUpd = array();
				foreach($widget_contents as $widget_content) {
					$league_link = $widget_content->find("a[class=league-link]", 0);
					if($league_link) {
						foreach($widget_content->find("ul", 0)->children() as $main_event_groups) {
						
							foreach($main_event_groups->find("ul", 0)->children() as $event_groups) {
						
								$insUpd = array();
								$insUpd["odds_league"] = Common::prepare_str($league_link->innertext);
								$insUpd["odds_html_id"] = $currhtml["html_id"];

								$bet = trim($main_event_groups->children(0)->innertext);
								//echo $bet."<br/>";

								if(in_array($bet, $getSzotar)) {
									$insUpd["odds_type"] = array_search($bet, $getSzotar);
									$insUpd["odds_type_text"] = $bet;
									$insUpd["odds_date"] = $this->_getDate($event_groups->find("h2[class=event-group-level1]", 0)->innertext);
									switch($insUpd["odds_type"]) {
										
										case "3W": {
											foreach($event_groups->find("table[class=options]") as $table) {
												$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$table->prev_sibling()->innertext);
												$insUpd["odds_team1"] = Common::prepare_str($table->find("span[class=option-name]", 0)->innertext);
												$insUpd["odds_team2"] = Common::prepare_str($table->find("span[class=option-name]", 2)->innertext);
												$insUpd["odds_odds1"] = Common::prepare_str($table->find("span[class=odds]", 0)->innertext);
												$insUpd["odds_odds2"] = Common::prepare_str($table->find("span[class=odds]", 1)->innertext);
												$insUpd["odds_odds3"] = Common::prepare_str($table->find("span[class=odds]", 2)->innertext);
												$this->odds_model->insert($insUpd);
												$this->insertedOdds++;
											}
											//Common::pr($insUpd);
											foreach($event_groups->find("h6[class=game]") as $ii => $game) {
												//echo $event_groups->innertext;
												
												$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$game->parent()->find("h6[class=event-info]", 0)->find("span", 0)->innertext);
												
												$insUpd["odds_team1"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 0)->plaintext);
												$insUpd["odds_team2"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 2)->plaintext);
												$insUpd["odds_odds1"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 0)->plaintext);
												$insUpd["odds_odds2"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 1)->plaintext);
												$insUpd["odds_odds3"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 2)->plaintext);
												//Common::pr($insUpd);
												$this->odds_model->insert($insUpd);
												$this->insertedOdds++;
											}
										} break;
										
										case "DC": {
											$insUpd["odds_html_id"] = $currhtml["html_id"];
											$insUpd["odds_date"] = $this->_getDate($event_groups->find("h2[class=event-group-level1]", 0)->innertext);
											foreach($event_groups->find("h2[class=event-group-level1]", 0)->next_sibling()->children() as $event) {
												$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext);
												foreach($event->find("h6[class=game]") as $game) {
													$insUpd["odds_type"] = array_search($bet, $getSzotar); // DCH felulirasok miatt
													if(in_array(trim($game->innertext), array("Halftime double chance", "Double chance", "Double Chance (regular time)"))) {
														$insUpd["odds_type_text"] = $game->innertext;
														if(trim($game->innertext) == "Halftime double chance") {
															$insUpd["odds_type"] .= "_H";
															// DC_H - Double Chance Halftime
														}
														$insUpd["odds_team1"] = str_replace(" or X", "", Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 0)->plaintext));
														$insUpd["odds_team2"] = str_replace("X or ", "", Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 1)->plaintext));
														$insUpd["odds_odds1"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 0)->plaintext);
														$insUpd["odds_odds2"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 1)->plaintext);
														$insUpd["odds_odds3"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 2)->plaintext);
														$this->odds_model->insert($insUpd);
														$this->insertedOdds++;
													}
												}
											}
										} break;
										
										case "HR": {
											$insUpd["odds_date"] = $this->_getDate($event_groups->find("h2[class=event-group-level1]", 0)->innertext);
											foreach($event_groups->find("h2[class=event-group-level1]", 0)->next_sibling()->children() as $event) {
												$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext);
												foreach($event->find("h6[class=game]") as $game) {
													$insUpd["odds_type"] = array_search($bet, $getSzotar);
													if(trim($game->innertext) == "Which team will win the 2nd half (only goals scored in 2nd half count)?" or trim($game->innertext) == "Result at half-time") {
														$insUpd["odds_type_text"] = $game->innertext;
														if(trim($game->innertext) == "Which team will win the 2nd half (only goals scored in 2nd half count)?") {
															$insUpd["odds_type"] .= "_2H";
															// HR_2H -  Halftime result 2nd half
														} else {
															$insUpd["odds_type"] .= "_H";
															// HR_H -  Halftime result 1st half
														}
														$insUpd["odds_team1"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 0)->plaintext);
														$insUpd["odds_team2"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 2)->plaintext);
														$insUpd["odds_odds1"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 0)->plaintext);
														$insUpd["odds_odds2"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 1)->plaintext);
														$insUpd["odds_odds3"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 2)->plaintext);
														$this->odds_model->insert($insUpd);
														$this->insertedOdds++;
													}
												}
												$this->odds_model->insert($insUpd);
												$this->insertedOdds++;
											}
										} break;
										
										case "NG": {
											$insUpd["odds_date"] = $this->_getDate($event_groups->find("h2[class=event-group-level1]", 0)->innertext);
											foreach($event_groups->find("h2[class=event-group-level1]", 0)->next_sibling()->children() as $event) {
												$teams = explode(" - ", str_replace($event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext, "", ($event->find("h6[class=event-info]", 0)->plaintext)));
												$insUpd["odds_team1"] = Common::prepare_str($teams[0]);
												$insUpd["odds_team2"] = Common::prepare_str($teams[count($teams)-1]);
												$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext);
												foreach($event->find("h6[class=game]") as $ii => $game) {
													if(in_array(trim($game->innertext), array("How many goals will be scored?", "How many goals will be scored in the 2nd half?", "How many goals will be scored in the 1st half?"))) {
														$odds_type_postfix = "";
														$insUpd["odds_type_text"] = $game->innertext;
														if(trim($game->innertext) == "How many goals will be scored in the 2nd half?") {
															$odds_type_postfix = "_2H";
															// NG_2H -  Number of goals 2nd half
														} elseif($game->innertext == "How many goals will be scored in the 1st half?") {
															$odds_type_postfix = "_H";
															// NG_H -  Number of goals 1st half
														}
														foreach($game->next_sibling()->find("span[class=option-name]") as $ii => $options) {
															//Common::pr($options); exit;
															$insUpd["odds_odds1"] = Common::prepare_str($options->prev_sibling()->plaintext);
															$type = trim($options->innertext);
															$type = str_replace(array(" or more", "No goal"), array("+", 0), $type);
															if(strpos($type, "Over ") !== FALSE or strpos($type, "Under ") !== FALSE) {
																$over = strpos($type, "Over");
																$type = str_replace(array("Over ", "Under "), array("", ""), $type).(($over !== FALSE)?("+"):("-"));
															}
															$insUpd["odds_type"] = array_search($bet, $getSzotar).$odds_type_postfix."_".$type;
															$this->odds_model->insert($insUpd);
															$this->insertedOdds++;
														}
													}
												}
											}
										} break;
										
										case "H/F": {
											$insUpd["odds_date"] = $this->_getDate($event_groups->find("h2[class=event-group-level1]", 0)->innertext);
											foreach($event_groups->find("h2[class=event-group-level1]", 0)->next_sibling()->children() as $event) {
												$teams = explode(" - ", str_replace($event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext, "", ($event->find("h6[class=event-info]", 0)->plaintext)));
												$insUpd["odds_team1"] = Common::prepare_str($teams[0]);
												$insUpd["odds_team2"] = Common::prepare_str($teams[count($teams)-1]);
												$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext);
												foreach($event->find("h6[class=game]") as $ii => $game) {
													if(in_array(trim($game->innertext), array("Halftime or Fulltime (bet wins if result is true either at halftime or fulltime)", "Halftime/Fulltime"))) {
														$insUpd["odds_type_text"] = $game->innertext;
														if(trim($game->innertext) == "Halftime or Fulltime (bet wins if result is true either at halftime or fulltime)") {
															$insUpd["odds_type"] = array_search($bet, $getSzotar)."_HoF"; // H/F_HoF - Halftime/Fulltime half or full time
															$insUpd["odds_team1"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 0)->plaintext);
															$insUpd["odds_team2"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 2)->plaintext);
															$insUpd["odds_odds1"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 0)->plaintext);
															$insUpd["odds_odds2"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 1)->plaintext);
															$insUpd["odds_odds3"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 2)->plaintext);
															$this->odds_model->insert($insUpd);
															$this->insertedOdds++;
														} else {
															foreach($game->next_sibling()->find("span[class=option-name]") as $ii => $options) {
																$insUpd = array(
																	"odds_date" => $insUpd["odds_date"],
																	"odds_team1" => $insUpd["odds_team1"],
																	"odds_team2" => $insUpd["odds_team2"],
																	"odds_datetime" => $insUpd["odds_datetime"],
																	"odds_type" => array_search($bet, $getSzotar),
																	"odds_league" => $insUpd["odds_league"],
																	"odds_html_id" => $insUpd["odds_html_id"],
																	"odds_type_text" => $insUpd["odds_type_text"],
																	"odds_odds1" => Common::prepare_str($options->prev_sibling()->plaintext),
																);
																$options = trim($options->innertext);
																$cnt = count($insUpd);
																switch(true) {
																	case ($options == $insUpd["odds_team1"]." / ".$insUpd["odds_team1"]): {
																		$insUpd["odds_type"] .= "_1_1";
																	} break;
																	case ($options == "X / ".$insUpd["odds_team1"]): {
																		$insUpd["odds_type"] .= "_X_1";
																	} break;
																	case ($options == $insUpd["odds_team2"]." / ".$insUpd["odds_team1"]): {
																		$insUpd["odds_type"] .= "_2_1";
																	} break;
																	case ($options == $insUpd["odds_team1"]." / X"): {
																		$insUpd["odds_type"] .= "_1_X";
																	} break;
																	case ($options == "X / X"): {
																		$insUpd["odds_type"] .= "_X_X";
																	} break;
																	case ($options == $insUpd["odds_team2"]." / X"): {
																		$insUpd["odds_type"] .= "_2_X";
																	} break;
																	case ($options == "X / ".$insUpd["odds_team2"]): {
																		$insUpd["odds_type"] .= "_X_2";
																	} break;
																	case ($options == $insUpd["odds_team1"]." / ".$insUpd["odds_team2"]): {
																		$insUpd["odds_type"] .= "_1_2";
																	} break;
																	case ($options == $insUpd["odds_team2"]." / ".$insUpd["odds_team2"]): {
																		$insUpd["odds_type"] .= "_2_2";
																	} break;
																}
																if($cnt < count($insUpd)) {
																	$this->odds_model->insert($insUpd);
																	$this->insertedOdds++;
																}
															}
														}
													};
												}
											}
										} break;
										
										case "GB": {
											$insUpd["odds_date"] = $this->_getDate($event_groups->find("h2[class=event-group-level1]", 0)->innertext);
											foreach($event_groups->find("h2[class=event-group-level1]", 0)->next_sibling()->children() as $event) {
												$teams = explode(" - ", str_replace($event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext, "", ($event->find("h6[class=event-info]", 0)->plaintext)));
												$insUpd["odds_team1"] = Common::prepare_str($teams[0]);
												$insUpd["odds_team2"] = Common::prepare_str($teams[count($teams)-1]);
												$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext);
												foreach($event->find("h6[class=game]") as $ii => $game) {
													if(in_array(trim($game->innertext), array("Goal bet (regular time)", "1st Half Goal Bet"))) {
														$type = "";
														if(trim($game->innertext) == "1st Half Goal Bet") {
															$type = "_H"; 
														}
														$insUpd["odds_type_text"] = $game->innertext;
														foreach($game->next_sibling()->find("span[class=option-name]") as $ii => $options) {
															$insUpd["odds_type"] = array_search($bet, $getSzotar).$type."_".trim($options->innertext);
															$insUpd["odds_odds1"] = Common::prepare_str($options->prev_sibling()->plaintext);
															$this->odds_model->insert($insUpd);
															$this->insertedOdds++;
														}
													}
												}
											}
										} break;
										
										case "HC": {
											$insUpd["odds_date"] = $this->_getDate($event_groups->find("h2[class=event-group-level1]", 0)->innertext);
											foreach($event_groups->find("h2[class=event-group-level1]", 0)->next_sibling()->children() as $event) {
												$teams = explode(" - ", str_replace($event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext, "", ($event->find("h6[class=event-info]", 0)->plaintext)));
												$insUpd["odds_team1"] = Common::prepare_str($teams[0]);
												$insUpd["odds_team2"] = Common::prepare_str($teams[count($teams)-1]);
												$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext);
												foreach($event->find("h6[class=game]") as $ii => $game) {
													$type = preg_split("/ /", trim($game->innertext));
													if(isset($type[1])) {
														$type = str_replace(":", "-", trim($type[1]));
														$insUpd["odds_type_text"] = $game->innertext;
														$insUpd["odds_type"] = array_search($bet, $getSzotar)."_".$type;
														$insUpd["odds_team1"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 0)->plaintext);
														$insUpd["odds_team2"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 1)->plaintext);
														$insUpd["odds_odds1"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 0)->plaintext);
														$insUpd["odds_odds2"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 1)->plaintext);
														$insUpd["odds_odds3"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 2)->plaintext);
														$this->odds_model->insert($insUpd);
														$this->insertedOdds++;
													}
												}
											}
										} break;
										
										// tennis
										case "2W": {
											$insUpd["odds_date"] = $this->_getDate($event_groups->find("h2[class=event-group-level1]", 0)->innertext);
											foreach($event_groups->find("h2[class=event-group-level1]", 0)->next_sibling()->children() as $event) {
												//Common::pr($event->innertext); exit(0);
												if(is_object($insUpd["odds_date"]." ".$event->find("h6[class=event-info]", 0))) {
													$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext);
												} else {
													$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$event->find("h6", 0)->innertext);
												}
												//Common::pr($insUpd);
												//exit($event->innertext);
												foreach($event->find("h6[class=game]") as $ii => $game) {
													$insUpd["odds_type_text"] = $game->innertext;
													$insUpd["odds_type"] = array_search($bet, $getSzotar);
													$insUpd["odds_team1"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 0)->plaintext);
													$insUpd["odds_team2"] = Common::prepare_str($game->next_sibling()->find("span[class=option-name]", 1)->plaintext);
													$insUpd["odds_odds1"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 0)->plaintext);
													$insUpd["odds_odds2"] = NULL;
													$insUpd["odds_odds3"] = Common::prepare_str($game->next_sibling()->find("span[class=odds]", 1)->plaintext);
													$this->odds_model->insert($insUpd);
													$this->insertedOdds++;
												}
												foreach($event->find("table[class=options]") as $ii => $game) {
													$insUpd["odds_type"] = array_search($bet, $getSzotar);
													$insUpd["odds_team1"] = Common::prepare_str($game->find("span[class=option-name]", 0)->plaintext);
													$insUpd["odds_team2"] = Common::prepare_str($game->find("span[class=option-name]", 1)->plaintext);
													$insUpd["odds_odds1"] = Common::prepare_str($game->find("span[class=odds]", 0)->plaintext);
													$insUpd["odds_odds2"] = NULL;
													$insUpd["odds_odds3"] = Common::prepare_str($game->find("span[class=odds]", 1)->plaintext);
													//Common::pr($insUpd);
													$this->odds_model->insert($insUpd);
													$this->insertedOdds++;
												}
											}
										} break;
										
										// basketball
										// baseball
										case "ML": {
											$insUpd["odds_date"] = $this->_getDate($event_groups->find("h2[class=event-group-level1]", 0)->innertext);
											foreach($event_groups->find("h2[class=event-group-level1]", 0)->next_sibling()->children() as $event) {
												$teams = explode(" - ", str_replace($event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext, "", ($event->find("h6[class=event-info]", 0)->plaintext)));
												$insUpd["odds_type_text"] = $main_event_groups->find("h2[class=event-group-level2]", 0)->innertext;
												$insUpd["odds_team1"] = Common::prepare_str($teams[0]);
												$insUpd["odds_team2"] = Common::prepare_str($teams[count($teams)-1]);
												$insUpd["odds_datetime"] = Common::prepare_str($insUpd["odds_date"]." ".$event->find("h6[class=event-info]", 0)->find("span[class=event-header-level0]", 0)->innertext);
												foreach($event->find("table[class=options-wide]") as $game) {
													$insUpd["odds_type"] = array_search($bet, $getSzotar);
													$insUpd["odds_team1"] = Common::prepare_str($game->find("span[class=option-name]", 0)->plaintext);
													$insUpd["odds_team2"] = Common::prepare_str($game->find("span[class=option-name]", 1)->plaintext);
													$insUpd["odds_odds1"] = Common::prepare_str($game->find("span[class=odds]", 0)->plaintext);
													$insUpd["odds_odds2"] = NULL;
													$insUpd["odds_odds3"] = Common::prepare_str($game->find("span[class=odds]", 1)->plaintext);
													//Common::pr($insUpd);
													$this->odds_model->insert($insUpd);
													$this->insertedOdds++;
												}
											}
										} break;
										
										// baseball
									}
								}
								
							}
							
						}
					}
				}
				
				/*              
                // html update
                $this->html_model->update($currhtml["html_id"], array(
					"html_parsed_time" => Common::now(),
					"html_inserted_odds" => $this->insertedOdds,
                ));
				//exit;
                */
				
				$html->clear(); 
				unset($html);
				$this->benchmark->mark("#".$currhtml["html_id"]."_end");
			}
		}

	}

	function _getDate($str) {
		$str = explode("-",$str);
		if(count($str) == 2) {
			$str = explode("/", $str[1]);
			if(count($str) == 3) {
				return trim($str[2])."-".str_pad(trim($str[0]), 2, 0, STR_PAD_LEFT)."-".str_pad(trim($str[1]), 2, 0, STR_PAD_LEFT);
			}
		}
		return NULL;
	}

}